/*
 * SavePlaylistDialog.java
 *
 * Created on October 15, 2008, 8:19 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.music.MusicObjects;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.bjj.utils.Resources.LABEL_KEY;
import com.bff.bjj.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import org.bff.slimserver.musicobjects.SlimSavedPlaylist;

/**
 *
 * @author  Bill
 */
public class SavePlaylistDialog extends MusicJDialog {

    private boolean createEmpty;

    /** Creates new form SavePlaylistDialog */
    public SavePlaylistDialog(java.awt.Frame parent) {
        this(parent, false);
    }

    /** Creates new form SavePlaylistDialog */
    public SavePlaylistDialog(java.awt.Frame parent, boolean createEmpty) {
        super(parent);
        initComponents();
        Utils.centerInParent(this, parent);
        this.createEmpty = createEmpty;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new BJJPanel();
        jLabel1 = new javax.swing.JLabel();
        tfName = new javax.swing.JTextField();
        lblMessage = new javax.swing.JLabel();
        btnPanel = new BJJPanel();
        btnSave = new BJJButton();
        btnCancel = new BJJButton();
        topPanel = new BJJPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Save Playlist");
        setMinimumSize(new java.awt.Dimension(300, 99));

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Playlist Name:");
        jPanel1.add(jLabel1, new java.awt.GridBagConstraints());

        tfName.setColumns(20);
        jPanel1.add(tfName, new java.awt.GridBagConstraints());

        lblMessage.setForeground(ColorProperties.INFO_MESSAGE_FORGROUND);
        lblMessage.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(lblMessage, gridBagConstraints);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        btnPanel.add(btnSave);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        btnPanel.add(btnCancel);

        getContentPane().add(btnPanel, java.awt.BorderLayout.SOUTH);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Save Playlist");
        topPanel.add(jLabel2);

        getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    setCursor(Utils.getHourglassCursor());
    try {
        String name = tfName.getText().trim();
        if (name.isEmpty()) {
            lblMessage.setText(getResource(LABEL_KEY.SAVE_PLAYLIST_NO_NAME));
        } else {
            try {
                List<SlimSavedPlaylist> playlistList =
                        new ArrayList<SlimSavedPlaylist>(MusicObjects.getSavedPlaylists());
                boolean found = false;
                for (SlimSavedPlaylist playlist : playlistList) {
                    if (playlist.getName().equals(name)) {
                        found = true;
                        break;
                    }
                }

                if (found) {
                    lblMessage.setText(getResource(LABEL_KEY.SAVE_PLAYLIST_EXISTS));
                } else {
                    if (isCreateEmpty()) {
                        JukeboxProperties.getInstance().getPlaylistManager().createEmptyPlaylist(name);
                    } else {
                        JukeboxProperties.getInstance().getPlaylist().savePlaylist(name);
                    }
                    dispose();
                }
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    } finally {
        setCursor(Utils.getNormalCursor());
    }

}//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JPanel btnPanel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JTextField tfName;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the createEmpty
     */
    public boolean isCreateEmpty() {
        return createEmpty;
    }

    /**
     * @param createEmpty the createEmpty to set
     */
    public void setCreateEmpty(boolean createEmpty) {
        this.createEmpty = createEmpty;
    }
}
